from xml.dom.minidom import parse

from xlstoxml import write_to_string_file

"""
根据key提取AOSP指定模块的字符串，并合入origin 资源中
"""
languages_map = {'ar': 'ar', 'cs': 'cs', 'da': 'da', 'de': 'de', 'el': 'el',
                 'es': 'es', 'fi': 'fi', 'fr': 'fr', 'fr-rCA': 'fr-rCA', 'hu': 'hu',
                 'it': 'it', 'iw': 'iw', 'ja': 'ja', 'ko': 'ko', 'nl': 'nl',
                 'nb': 'nb', 'pl': 'pl', 'pt': 'pt', 'pt-rBR': 'pt-rBR', 'ro': 'ro',
                 'ru': 'ru', 'sk': 'sk', 'sv': 'sv', 'th': 'th', 'zh-rCN': 'zh',
                 'zh-rTW': 'zh-rTW'}

key_list = ['wifi_check_password_try_again', 'wifi_disabled_password_failure', 'wifi_disabled_network_failure']
aosp_module_path = '/home/nan/Project/Mini/android/frameworks/base/packages/SettingsLib/res/'


def main():
    for lan in languages_map.keys():
        lan_file = aosp_module_path + 'values-' + lan + '/strings.xml'
        lans_dict = {}
        try:
            dom = parse(lan_file)
            for key in key_list:
                for node in dom.getElementsByTagName('string'):
                    if node.getAttribute('name') == key:
                        lans_dict[key] = node.childNodes[0].nodeValue.strip('"')
            write_to_string_file('res/origin/values-' + languages_map[lan] + '/strings.xml', lans_dict)
        except FileNotFoundError:
            print('WARNING:', 'miss res file ' + lan_file + ', current name:' + key)


if __name__ == '__main__':
    main()
